package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianLogClient;
import com.diandian.backend.entrance.client.dto.log.*;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterLogService {

    private static final Logger logger = LoggerFactory.getLogger(RouterLogService.class);

    @Autowired
    private DianDianLogClient dianDianLogClient;

    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data) {

        // 新增日志
        if(EntranceConstant.ROUTER_LOG_ADD.equals(cmd)){

            logger.info("执行新增日志请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogRecordAddDTO logRecordAddDTO = JSONObject.parseObject(data.toString(), LogRecordAddDTO.class);
            logRecordAddDTO.setClientId(belong);

            // 执行短信发送
            BaseResult resultDTO = dianDianLogClient.add(logRecordAddDTO);

            logger.info("新增日志请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 查询日志
        if(EntranceConstant.ROUTER_LOG_QUERY_PAGE.equals(cmd)){

            logger.info("执行分页查询日志请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogRecordPageDTO logRecordPageDTO = JSONObject.parseObject(data.toString(), LogRecordPageDTO.class);
            logRecordPageDTO.setClientIdEql(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.page(logRecordPageDTO);

            logger.info("分页查询日志请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-分页查询
        if(EntranceConstant.ROUTER_LOG_TASK_QUERY_PAGE.equals(cmd)){

            logger.info("执行任务日志-分页查询请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskPageParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskPageParamDTO.class);
            requestData.setBelongEql(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskPage(requestData);

            logger.info("执行任务日志-分页查询结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-查询详情
        if(EntranceConstant.ROUTER_LOG_TASK_QUERY_DETAIL.equals(cmd)){

            logger.info("执行任务日志-查询详情请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskDetailParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskDetailParamDTO.class);
            requestData.setBelong(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskGetDetail(requestData);

            logger.info("执行任务日志-查询详情结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-修改数据(仅待执行的允许修改）
        if(EntranceConstant.ROUTER_LOG_TASK_ALTER.equals(cmd)){

            logger.info("执行任务日志-修改数据请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskUpdateReadyParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskUpdateReadyParamDTO.class);
            requestData.setBelong(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskUpdateReady(requestData);

            logger.info("执行任务日志-修改数据结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-修改操作状态
        if(EntranceConstant.ROUTER_LOG_TASK_STATUS_ALTER.equals(cmd)){

            logger.info("执行任务日志-修改操作状态请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskUpdateStatusParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskUpdateStatusParamDTO.class);
            requestData.setBelong(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskUpdateStatus(requestData);

            logger.info("执行任务日志-修改操作状态结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-删除数据(仅待执行的允许修改）
        if(EntranceConstant.ROUTER_LOG_TASK_DELETE.equals(cmd)){

            logger.info("执行任务日志删除数据请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskDeleteReadyParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskDeleteReadyParamDTO.class);
            requestData.setBelong(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskDeleteReady(requestData);

            logger.info("执行任务日志删除数据结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-新增任务
        if(EntranceConstant.ROUTER_LOG_TASK_ADD.equals(cmd)){

            logger.info("执行任务日志新增任务请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskAddParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskAddParamDTO.class);
            requestData.setBelong(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskAdd(requestData);

            logger.info("执行任务日志新增任务结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-进程-分页查询
        if(EntranceConstant.ROUTER_LOG_TASK_PROCESS_QUERY_PAGE.equals(cmd)){

            logger.info("执行任务日志-进程-分页查询请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskProcessPageParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskProcessPageParamDTO.class);
            requestData.setBelongEql(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskProcessPage(requestData);

            logger.info("执行任务日志-进程-分页查询结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 任务日志-进程-列表查询
        if(EntranceConstant.ROUTER_LOG_TASK_PROCESS_QUERY_LIST.equals(cmd)){

            logger.info("执行任务日志-进程-列表查询请求，参数: {}", data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            LogTaskProcessListParamDTO requestData = JSONObject.parseObject(data.toString(), LogTaskProcessListParamDTO.class);
            requestData.setBelongEql(belong);

            // 执行请求
            BaseResult resultDTO = dianDianLogClient.logTaskProcessList(requestData);

            logger.info("执行任务日志-进程-列表查询结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
